package org.csu.gp_store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.gp_store.entity.AccountAddress;
import org.csu.gp_store.mapper.AccountAddressMapper;
import org.csu.gp_store.service.AccountAddressService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountAddressServiceImpl implements AccountAddressService {

    @Autowired
    AccountAddressMapper accountAddressMapper;

    @Override
    public List<AccountAddress> getAddressByAccountID(@NotNull Long accountID) {
        LambdaQueryWrapper<AccountAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AccountAddress::getAccountID,accountID);
        return accountAddressMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public AccountAddress getAddressByID(@NotNull Long indexID) {
        LambdaQueryWrapper<AccountAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AccountAddress::getIndexID,indexID);
        List<AccountAddress> addressList = accountAddressMapper.selectList(lambdaQueryWrapper);
        if(addressList.size()==1){
            return addressList.get(0);
        }
        return null;
    }

    @Override
    public int updatePhone(@NotNull Long indexID,@NotNull String phone) {
        UpdateWrapper<AccountAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(AccountAddress::getIndexID,indexID).set(AccountAddress::getPhone,phone);
        return accountAddressMapper.update(null,updateWrapper);
    }

    @Override
    public int updateName(@NotNull Long indexID, String name) {
        UpdateWrapper<AccountAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(AccountAddress::getIndexID,indexID).set(AccountAddress::getName,name);
        return accountAddressMapper.update(null,updateWrapper);
    }

    @Override
    public int updateAddress(@NotNull Long indexID, String address) {
        UpdateWrapper<AccountAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(AccountAddress::getIndexID,indexID).set(AccountAddress::getAddress,address);
        return accountAddressMapper.update(null,updateWrapper);
    }

    @Override
    public int insertAccountAddress(AccountAddress accountAddress) {
        LambdaQueryWrapper<AccountAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(accountAddress.getAccountID()!=null,AccountAddress::getAccountID,accountAddress.getAccountID());
        lambdaQueryWrapper.eq(accountAddress.getAddress()!=null,AccountAddress::getAddress,accountAddress.getAddress());
        List<AccountAddress> addressList = accountAddressMapper.selectList(lambdaQueryWrapper);
        if(addressList.size()==0){
            return accountAddressMapper.insert(accountAddress);
        }
        return 0;
    }

    @Override
    public int deleteAccountAddress(@NotNull Long indexID) {
        LambdaQueryWrapper<AccountAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AccountAddress::getIndexID,indexID);
        return accountAddressMapper.delete(lambdaQueryWrapper);
    }


}
